package com.kevin.concurrent;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author Kevin Liu
 * @date 2020/9/27 5:23 下午
 */
public class Second {

    private static final int MAX_SIZE = 10;

    private static final Queue<Integer> QUEUE = new LinkedList<>();

    public static void main(String[] args) {
        Thread threadA = new Thread(() -> {
            while (true) {
                synchronized (QUEUE) {
                    while (QUEUE.size() == MAX_SIZE) {
                        try {
                            QUEUE.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("ThreadA生产了一条数据");
                    QUEUE.add(1);
                    QUEUE.notifyAll();
                }
            }
        });
        Thread threadB = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            while (true)  {
                synchronized (QUEUE) {
                    while (QUEUE.size() == 0) {
                        try {
                            QUEUE.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("ThreadB消费了一条数据 " + QUEUE.poll());
                    QUEUE.notifyAll();
                }
            }
        });
        threadA.start();
        threadB.start();
    }
}
